<?php

//class Model_Post extends ActiveRecord\Model {
class Model_Post {

    protected static $table = 'posts';

    //protected $belongs_to = array('category');

    public static function get_latest($limit = NULL, $offset = NULL)
    {
        $query = DB::select()->from(self::$table)->order_by('published', 'DESC');

        if (is_int($limit))
        {
            $query->limit($limit);
        }

        if (is_int($offset))
        {
            $query->offset($offset);
        }

        return $query;
    }

    public static function get_by_id($id)
    {
        return DB::select()->from(self::$table)->where('id', '=', $id)->limit(1);
    }

    public static function count()
    {
        return DB::select(array('COUNT("id")', 'total_posts'))->from(self::$table)->execute()->get('total_posts', 0);
    }
}
